<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of My
 *
 * @author Kam
 */
class My_Utils {
    // offer

    const PROP_OFFER_ACTIVE_DAYS = 'active_days';
    const PROP_OFFER_WYSWIG = 'wyswig';
    const PROP_OFFER_DESC_LIMIT = 'desc_limit';
    const PROP_OFFER_PHOTOS_LIMIT = 'photos_limit';
    const PROP_OFFER_PRICE = 'price';

    // catalog
    const PROP_CATALOG_WYSWIG = 'wyswig';
    const PROP_CATALOG_DESC = 'description';
    const PROP_CATALOG_DESC_LIMIT = 'desc_limit';
    const PROP_CATALOG_PHOTOS_LIMIT = 'photos_limit';
    const PROP_CATALOG_PRICE = 'price';

    private static $_translate;
    private static $_currentUser;
    private static $_contentHeight = 0;
    private static $_properties;

    public static function getProperty($name) {
        $props = self::getProperties();
        if (isset($props[$name])) {
            if($props[$name]['type'] == 'boolean') {
                return $props[$name]['value'] ? true : false;
            }
            return $props[$name]['value'];
        }
        return null;
    }

    public static function getProperties() {
        if (self::$_properties == null) {
            $daoProperty = new Application_Model_DAO_Property();
            self::$_properties = $daoProperty->getProperties();
        }
        return self::$_properties;
    }

    public static function addContentHeight($h) {
        return self::$_contentHeight += $h;
    }

    public static function getContentHeight($h) {
        return self::$_contentHeight;
    }

    public static function removeFileOrDir($path) {
        if (@is_file($path)) {
            @unlink($path);
        } else {
            foreach (glob($path . '/*') as $file) {
                if (is_dir($file)) {
                    rrmdir($file);
                } else {
                    unlink($file);
                }
            } rmdir($path);
        }
    }

    public static function getImageUploadedPath() {
        $path = realpath(APPLICATION_PATH . '/../uploaded/image');
        return $path;
    }

    public static function getImagePublicCachePath() {
        $path = realpath(APPLICATION_PUBLIC . '/images');
        return $path;
    }

    public static function buildTempPath($token = null, $categoryDir = null) {
        if (empty($token)) {
            $token = session_id();
        }
        $date = date('YmdHis');
        $uploadDir = APPLICATION_PATH . '/../uploaded/' . (empty($categoryDir) ? '' : trim($categoryDir, '/\\ ') . '/') . 'tmp/' . $date . '-' . $token;
        @mkdir($uploadDir);
        return realpath($uploadDir);
    }

    public static function getCatalogProperties() {
        return array(
            Application_Model_DAO_Catalog::TYPE_FREE => array(
                self::PROP_CATALOG_WYSWIG => self::getProperty('catalog.free.'.self::PROP_CATALOG_WYSWIG),
                self::PROP_CATALOG_DESC => self::getProperty('catalog.free.'.self::PROP_CATALOG_DESC),
                self::PROP_CATALOG_DESC_LIMIT => self::getProperty('catalog.free.'.self::PROP_CATALOG_DESC_LIMIT),
                self::PROP_CATALOG_PHOTOS_LIMIT => self::getProperty('catalog.free.'.self::PROP_CATALOG_PHOTOS_LIMIT),
                self::PROP_CATALOG_PRICE => self::getProperty('catalog.free.'.self::PROP_CATALOG_PRICE),
            ),
            Application_Model_DAO_Catalog::TYPE_PAID => array(
                self::PROP_CATALOG_DESC => self::getProperty('catalog.paid.'.self::PROP_CATALOG_DESC),
                self::PROP_CATALOG_WYSWIG => self::getProperty('catalog.paid.'.self::PROP_CATALOG_WYSWIG),
                self::PROP_CATALOG_DESC_LIMIT => self::getProperty('catalog.paid.'.self::PROP_CATALOG_DESC_LIMIT),
                self::PROP_CATALOG_PHOTOS_LIMIT => self::getProperty('catalog.paid.'.self::PROP_CATALOG_PHOTOS_LIMIT),
                self::PROP_CATALOG_PRICE => self::getProperty('catalog.paid.'.self::PROP_CATALOG_PRICE),
            ),
        );
    }

    public static function getOfferProperties() {
        return array(
            Application_Model_DAO_Offer::TYPE_FREE => array(
                self::PROP_OFFER_ACTIVE_DAYS => self::getProperty('offer.free.'.self::PROP_OFFER_ACTIVE_DAYS),
                self::PROP_OFFER_WYSWIG => self::getProperty('offer.free.'.self::PROP_OFFER_WYSWIG),
                self::PROP_OFFER_DESC_LIMIT => self::getProperty('offer.free.'.self::PROP_OFFER_DESC_LIMIT),
                self::PROP_OFFER_PHOTOS_LIMIT => self::getProperty('offer.free.'.self::PROP_OFFER_PHOTOS_LIMIT),
                self::PROP_OFFER_PRICE => self::getProperty('offer.free.'.self::PROP_OFFER_PRICE),
            ),
            Application_Model_DAO_Offer::TYPE_PAID => array(
                self::PROP_OFFER_ACTIVE_DAYS => self::getProperty('offer.paid.'.self::PROP_OFFER_ACTIVE_DAYS),
                self::PROP_OFFER_WYSWIG => self::getProperty('offer.paid.'.self::PROP_OFFER_WYSWIG),
                self::PROP_OFFER_DESC_LIMIT => self::getProperty('offer.paid.'.self::PROP_OFFER_DESC_LIMIT),
                self::PROP_OFFER_PHOTOS_LIMIT => self::getProperty('offer.paid.'.self::PROP_OFFER_PHOTOS_LIMIT),
                self::PROP_OFFER_PRICE => self::getProperty('offer.paid.'.self::PROP_OFFER_PRICE),
            ),
        );
    }

    public static function getCurrentUserId() {
        if (empty(self::$_currentUser)) {
            return 0;
        }
        return self::$_currentUser['user_id'];
    }

    public static function getCurrentUser() {
        return self::$_currentUser;
    }

    public static function isAdmin() {
        $user = self::getCurrentUser();
        if (empty($user)) {
            return false;
        }
        return $user['role'] == 'admin';
    }

    public static function setCurrentUser($user) {
        self::$_currentUser = $user;
    }

    public static function getRequestParam($key, $default = null) {
        return self::getController()->getRequest()->getParam($key, $default);
    }

    /**
     * 
     * @return My_Controller_Action
     */
    public static function getController() {
        return Zend_Registry::get('Controller');
    }

    public static function redirect($url, $options = array()) {
        self::getController()->_redirect($url, $options);
    }

    public static function getEventInfoTypes() {
        return array(
            'cinema' => _('XXX_ADMIN_EVENT_INFO_TYPE_CINEMA'),
            'theater' => _('XXX_ADMIN_EVENT_INFO_TYPE_THEATER'),
            'museum' => _('XXX_ADMIN_EVENT_INFO_TYPE_MUSEUM'),
            'concert' => _('XXX_ADMIN_EVENT_INFO_TYPE_CONCERT'),
            'sport' => _('XXX_ADMIN_EVENT_INFO_TYPE_SPORT'),
            'misc' => _('XXX_ADMIN_EVENT_INFO_TYPE_MISC'),
        );
    }

    public static function getEventPlacesTypes() {
        return array(
            'cinema' => _('XXX_ADMIN_EVENT_PLACE_TYPE_CINEMA'),
            'theater' => _('XXX_ADMIN_EVENT_PLACE_TYPE_THEATER'),
            'museum' => _('XXX_ADMIN_EVENT_PLACE_TYPE_MUSEUM'),
            'amphitheatre' => _('XXX_ADMIN_EVENT_PLACE_TYPE_AMPHITHEATRE'),
            'sport' => _('XXX_ADMIN_EVENT_PLACE_TYPE_SPORT'),
            'misc' => _('XXX_ADMIN_EVENT_PLACE_TYPE_MISC'),
        );
    }

    public static function translate($key, array $data = null) {
        if (self::$_translate == null) {
            self::$_translate = Zend_Registry::get('Zend_Translate');
        }
        $msg = self::$_translate->_($key);
        if ($data != null && !empty($data)) {
            $tmpkeys = array_keys($data);
            $vals = array_values($data);
            $keys = array();
            foreach ($tmpkeys as $tmpkey) {
                $keys[] = '%' . $tmpkey . '%';
            }
            $msg = str_replace($keys, $vals, $msg);
        }
        return $msg;
    }

}
